﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Helpers;
using System.ComponentModel.DataAnnotations;

namespace Model.Entities
{
    public class PublicationScope
    {
        [Key]
        public int PublicationScopeID { get; set; }
        public DateTime ValidFrom { get; set; }
        public DateTime ValidTo { get; set; }
        public ICollection<Recurrence> Recurrences { get; set; }

        public bool IsValid(DateTime date)
        {
            if (date >= ValidTo || date <= ValidFrom)
                return false;
            if (this.Recurrences != null)
            {
                foreach (var item in Recurrences)
                {
                    if (item.IsValid(date))
                        return true;
                }
                return false;
            }
            return true;
        }

        public void AddRecurrence(Recurrence recurrence)
        {
            if (this.Recurrences == null)
                this.Recurrences = new List<Recurrence>();
            this.Recurrences.Add(recurrence);
        }
    }
}
